<?php

class MySQL_Driver extends Database_Library {
    
    private static $db_host = "localhost";
    private static $db_user = "root";
    private static $db_pass = "root";
    protected $db_name = "calendaridaw2";
    
    protected function open_connection() {
        $this->conn = new mysqli(self::$db_host, self::$db_user, self::$db_pass, $this->db_name);
        
        # Comprobar si han habido errores en la conexion de la base de datos
        if (mysqli_connect_errno()) {
            printf("Conexión fallida: %s\n", mysqli_connect_error());
            exit();
        }
        
        return TRUE;
    }
    
    # Cerrar conexion a la BBDD
    protected function close_connection() {
        $this->conn->close();
        
        return TRUE;
    }
    
    # Metodo para consultar datos en la BBDD. No devuelve ni almacena nada.
    # Ejecuta la query a partir de la sentencia previamente almacenada en '$this->query'
    public function execute_single_query()  {
        $this->open_connection();
        $this->conn->query($this->query);
        $id_last_query = $this->conn->insert_id;

        $this->close_connection();
        
        return $id_last_query;
    }
    
    public function get_results_from_query() {
        $this->open_connection();
        $result = $this->conn->query($this->query);
        
        while($this->rows[] = $result->fetch_assoc());

        $result->close();
        $this->close_connection();
        
        # Visualizar resultados
//        foreach ($this->rows[0] as $key => $value) {
//            echo "Key: $key; Value: $value /***/ ";
//        }
        
        array_pop($this->rows);
        
        /**/
//      $comprobar_resultados = $this->rows;
//      echo implode(" ", $comprobar_resultados[0]);
//      echo "<br />";
//      echo $comprobar_resultados[0]["username"];
//      echo "<br />";
//      var_dump($comprobar_resultados);
        /**/
        
        return TRUE;
    }
    
}

?>
